package cn.com.dhcc.turbo.dao.registry;

import org.apache.log4j.Logger;
import org.springframework.stereotype.Repository;

import cn.com.dhcc.app.core.CM;
import cn.com.dhcc.app.core.dao.BaseDao;
import cn.com.dhcc.app.core.exception.DaoException;
import cn.com.dhcc.app.core.handler.modulehandler.WhereCondition;
import cn.com.dhcc.app.core.handler.pagerhandler.Pager;
import cn.com.dhcc.app.core.handler.pagerhandler.PagerData;
import cn.com.dhcc.app.core.service.Log;
import cn.com.dhcc.app.core.service.Log.LOGTYPE;
import cn.com.dhcc.turbo.entity.registry.ExchConnRequest;
import cn.com.dhcc.turbo.entity.registry.wapper.ExchConnRequestRowWapper;

/**
 * ExchConnRequestDao
 * 
 * @日期：2014-5-19 下午2:01:55
 * @作者：张洪涛
 * @版权所有：东华软件
 * @版本：1.0
 */
@Repository
public class ExchConnRequestDao extends BaseDao {
	@SuppressWarnings("deprecation")
	private static Logger logger = Log.getExistLogger(LOGTYPE.SAI);

	/**
	 * 获取请求审核信息列表
	 * 
	 * @param pager
	 * @param condition
	 * @return
	 * @throws DaoException
	 */
	public PagerData<ExchConnRequest> list(Pager pager, WhereCondition condition)
			throws DaoException {
		String msg = "";
		String sql = "select * from  " + SI_TABLE("turbo_exch_con_request");
		try {
			return CM.getDao().getPagerList("获取请求审核信息列表", sql,
					new ExchConnRequestRowWapper(), pager, condition,
					new Object[] {});
		} catch (DaoException e) {
			msg = "获取请求审核列表失败";
			logger.error(msg, e);
			throw new DaoException(msg, e);
		}
	}

	public void add(ExchConnRequest vo) throws DaoException {
		String msg = "";
		try {
			CM.getDao().insertModule("添加请求审核", vo);
		} catch (DaoException e) {
			msg = "添加请求审核失败";
			logger.error(msg, e);
			throw new DaoException(msg, e);
		}
	}

	public ExchConnRequest get(String id) throws DaoException {
		String msg = "";
		String sql = "select * from  " + SI_TABLE("turbo_exch_con_request")
				+ " where id=?";
		try {
			return CM.getDao().get("获取请求审核", sql,
					new ExchConnRequestRowWapper(), new Object[] { id });
		} catch (DaoException e) {
			msg = "获取请求审核失败";
			logger.error(msg, e);
			throw new DaoException(msg, e);
		}
	}

	/**
	 * 审核请求
	 * 
	 * @param vo
	 * @throws DaoException
	 */
	public void auditConnRequest(ExchConnRequest vo) throws DaoException {
		String msg = "";
		String sql = "update   " + SI_TABLE("turbo_exch_con_request")
				+ " set response=?,status=?, mtime=? where id=?";
		try {
			CM.getDao().update(
					"获取请求审核",
					sql,
					new Object[] { vo.getResponse(), vo.getStatus(),
							vo.getMtime(), vo.getId() });
		} catch (DaoException e) {
			msg = "审核失败";
			logger.error(msg, e);
			throw new DaoException(msg, e);
		}
	}

	/**
	 * 获取待审核总数
	 * 
	 * @return
	 * @throws DaoException
	 */
	public int getCountRequest() throws DaoException {
		String msg = "";
		String sql = "select count(*) from "
				+ SI_TABLE("turbo_exch_con_request")
				+ " where status= '2' and islocal='0'";
		try {
			return CM.getDao().queryForInt("获取待审核总数", sql, new Object[] {});
		} catch (DaoException e) {
			msg = "获取待审核总数失败";
			logger.error(msg, e);
			throw new DaoException(msg, e);
		}
	}

	/**
	 * 审核结果
	 * 
	 * @param vo
	 * @return
	 * @throws DaoException
	 */
	public int update(ExchConnRequest vo) throws DaoException {
		String msg = "";
		String sql = "update  " + SI_TABLE("turbo_exch_con_request")
				+ " set status=?,mtime=?,response=? where id=?";
		try {
			return CM.getDao().update(
					"审核结果",
					sql,
					new Object[] { vo.getStatus(), vo.getMtime(),
							vo.getResponse(), vo.getId() });
		} catch (DaoException e) {
			msg = "审核失败";
			logger.error(msg, e);
			throw new DaoException(msg, e);
		}
	}

	public void timingDelete(String day) throws DaoException {
		String msg = "";
		String sql = "delete  from " + SI_TABLE("turbo_exch_con_request")
				+ " where (sysdate - mtime) >? and (status='1' or  status='0')";
		try {
			CM.getDao().delete("定时删除已审核记录", sql, new Object[] {day});
		} catch (DaoException e) {
			msg = "定时删除已审核记录失败";
			logger.error(msg, e);
			throw new DaoException(msg, e);
		}
	}

}
